########################################################
########################################################
## Replication Code for LCA Analysis and Estimation of Individual Fixed Effects 
########################################################
########################################################



library(plyr)
library(reshape2)
library(IRanges)
library(ggplot2)
library(igraph)
library(Cairo)
library(data.table)
library(lubridate)
library(foreign)
library(openxlsx)
library(poLCA)
library(gridExtra)
library(scales)

Sys.setlocale(,"CHS")


dc<-read.csv("./Replication files/data_comments.csv")





dc$selfdisc<-as.numeric(grepl("要求严格|要求比较严格|廉洁自律|以身作则",dc$c.from.org))

dc$silu<-as.numeric(grepl("思路清晰|思路清楚|抓大事|重点突出|谋大局|谋长远|重点突出|抓重点|抓住重点|抓关键|议大事",dc$c.from.org))

dc$learn<-as.numeric(grepl("学习|思考|好学|钻研",dc$c.from.org))

dc$prioritize<-as.numeric(grepl("抓大事|重点突出|谋大局|谋长远|重点突出|抓重点|抓住重点|抓关键|议大事",dc$c.from.org))


dc$bold<-as.numeric(grepl("强烈的事业心|热情高|干劲大|魄力|敢抓敢管|魄力|果断|雷厉风行|敢于担当|敢于负责",dc$c.from.org))


dc$tuanjie<-as.numeric(grepl("领导艺术|团结同志一道|听取他人意见|不同意见|各方面的意见|讲究方法|工作方法|充分发挥班子成员|认真听取意见|注意听取|各方面意见|集体研究|听取意见|集体讨论|大家意见",dc$c.from.org))



dc$innovate<-as.numeric(grepl("视野开阔|知识面|改革创新意识|开拓创新精神|勇于开拓|创新意识|勇于开拓|开拓进取|创造性地|开拓创新|开拓进取|改革创新|勇于创新",dc$c.from.org))

dc$diligent<-as.numeric(grepl("勤恳|勤奋|兢兢业业|任劳任怨|敬业|勤政",dc$c.from.org))

dc$mass<-as.numeric(grepl("人民群众疾苦|关心群众|对群众有感情|群众有深厚感情|人民群众怀有深厚感情|任劳任怨|联系群众",dc$c.from.org))

dc$qianxu<-as.numeric(grepl("谦虚|谨慎|平易近人|不事张扬|谦和|宽厚|谦逊|忠厚|细致",dc$c.from.org))

dc$steady<-as.numeric(grepl("稳妥|稳重|不骄不躁|沉稳|处事稳健|低调",dc$c.from.org))


dc$pushy<-as.numeric(grepl("推动工作力度大|推动工作的力度大|推动工作有力|推进工作力度大|对工作要求高|工作节奏快",dc$c.from.org))

dc$frank<-as.numeric(grepl("不回避矛盾|开展批评|自我批评|直率|耿直|坦荡|坦诚|直爽",dc$c.from.org))

dc$careful<-as.numeric(grepl("严谨细致|周到|细致|考虑问题全面|考虑问题比较全面|严谨",dc$c.from.org))

dc$compatible<-as.numeric(grepl("亲和力|相容性|相融性|包容性|真诚友善|宽厚|关系融洽|合作共事|宽以待人|团结人",dc$c.from.org))


## less work-related or little variations
dc$loyal<-as.numeric(grepl("高度一致|保持一致|坚决贯彻|党性|大政方针|党和国家|对党|大局意识|",dc$c.from.org))


dc$zuzhi<-as.numeric(grepl("组织领导能力|驾驭全局|复杂局面|重大复杂|协调能力|处理复杂棘手问题|组织领导和协调|处理复杂问题|处理复杂棘手问题|组织协调|组织领导",dc$c.from.org))


dc$moral<-as.numeric(grepl("要求严格|要求比较严格|廉洁自律|以身作则|正派|公道|律己|自律|严格要求|正直|坚持原则|原则性强|原则性比较强|要求严格|要求比较严格|廉洁自律|以身作则|简朴",dc$c.from.org))

dc$pushy2<-as.numeric(grepl("推动工作力度大|推动工作的力度大|推动工作有力|推进工作力度大|对工作要求高|工作节奏快|强烈的事业心|热情高|干劲大|魄力|敢抓敢管|魄力|果断|雷厉风行|敢于担当|敢于负责",dc$c.from.org))



############ LCA Analysis ##################

set.seed(1111) 


v4 = poLCA(cbind(silu=silu+1,tuanjie=tuanjie+1,innovate=innovate+1,diligent=diligent+1, qianxu=qianxu+1,steady=steady+1,pushy2=pushy2+1,frank=frank+1,careful=careful+1,compatible=compatible+1)~comer+gvn,
           maxiter=50000, nclass=2,
           nrep=10,data=dc)
plot(v4)
dc$class1<-v4$predclass
dc$prob.type1<-v4$posterior[,1]
dc$prob.type2<-v4$posterior[,2]

dc$tp<-1



##########  Choosing No. of Classes Based on Fitness #######

bic<-list()
set.seed(1111)
for(i in 1:6){
  temp<-poLCA(cbind(silu=silu+1,tuanjie=tuanjie+1,innovate=innovate+1,diligent=diligent+1, qianxu=qianxu+1,steady=steady+1,pushy2=pushy2+1,frank=frank+1,careful=careful+1,compatible=compatible+1)~comer+gvn,
              maxiter=50000, nclass=i,
              nrep=10,data=dc)
  bic[[i]]<-temp$bic
  
}

dfit<-data.frame(class=1:6,bic=unlist(bic))

dfit2<-melt(dfit,id.var="class")
gb<-ggplot(dfit2[which(dfit2$variable=="bic"),],aes(x=class,y=value))+geom_line(size=.5)+ggtitle("Bayesian Information Criteria")+
  ylab("")+xlab("No. of Latent Class")


pdf(paste(target,"fit.pdf",sep="/"),height=4,width=6)
gb
dev.off()

#######################################################
#### Figure 1: Conditional Probabilities for Types ####
#######################################################
dimension<-rep(names(v4$probs),each=2)
type<-rep(c("collegial","assertive"),length(v4$probs))
prob<-unlist(lapply(v4$probs,function(x) x[,2]))

dp<-data.frame(cbind(dimension,type,prob))
dp$prob<-as.numeric(as.character(dp$prob))

###################
# Setting graphical parameters 

theme_set(theme_bw() + theme(text=element_text(family="Times"),legend.position="bottom",legend.direction="horizontal",axis.title.x=element_text(face="bold",vjust=-.5),axis.title.y=element_text(face="bold",vjust=1),title=element_text(face="bold",size=16,vjust=1.5),axis.text.x=element_text(size=12),strip.text=element_text(size=13)))


windowsFonts(Times=windowsFont("TT Times New Roman"))

##---- Functions---##

listdele<-function(x){
  a<-x[complete.cases(x),]
  return(a)
}

vplayout <- function(x, y) viewport(layout.pos.row = x, layout.pos.col = y)

# functions for r/cbinding gtables
#source("(FUN) bind gtables.r")



pd<-position_dodge(width=0.4)

cl<-c("seagreen","red","steelblue")

#################################

### Plotting ####

#dp<-read.csv("out3.csv",stringsAsFactors=F)
dx<-dp[which(dp$type=="assertive"),c("prob","dimension")]
dy<-dp[which(dp$type=="collegial"),c("prob","dimension")]
dp$title<-factor(dp$type,labels=c("Class 1 (Autocratic Style)","Class 2 (Collegial Style)"))

diff<-dx$prob-dy$prob

dp$dimension<-factor(dp$dimension, levels=dx$dimension[order(diff)],labels=c('"humble", "low-key", "modest", "gentle"',
                                                                             '"careful and cautious", "careful and thorough",\n "rigorous", "considers all aspects before action" ',
                                                                             '"steady", "reliable", "has a stable character",\n "moderate personality"',
                                                                             '"good at prioritizing tasks",\n "focuses on getting the most important task done",\n "focuses on the main tasks",\n "works with a clear mind"',
                                                                             '"maintains a cohesive standing committee",\n"comradely", "knows the art of leadership",\n "listens to different views"',
                                                                             '"compatible", "friendly", "cooperative",\n "tolerant and kind"',
                                                                             '"diligent", "hard working",\n "does not complain about work load",\n "solid work"',
                                                                             '"wide horizon", "visionary", "knowledgeable",\n "has an innovative spirit",\n "good at reform and innovation"' ,
                                                                             '"frank", "honest" "straightforward",\n "open", "direct",\n "not afraid of making criticism or self-criticism"' ,
                                                                             '"strong sense of responsibility",\n "enthusiastic about work", "resolute",\n "full of energy", "bold"' ))


gout<-ggplot(data=dp,aes(y=prob,x=dimension))+geom_bar(stat="identity" )+facet_grid(~title)+
  xlab("")+ylab("")+scale_y_continuous(labels=scales::percent)+coord_flip()+theme(axis.text.y=element_text(size=15))




############# Individual fixed effects ############# 
dc$year<-year(dc$date)

f<-lm(prob.type1~factor(leader)+factor(year)-1,data=dc)




lfe<-data.frame(lfe=coef(f)[grep("[\u4e00-\u9fff]",names(coef(f)))])
lfe$name<-substring(row.names(lfe),15)

write.dta(lfe,"leaderFE.dta")
